﻿using PIMS.Models.Base;
using PIMS.Utils;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PIMS.Utils
{
    public class DynamicObjectHelper1
    {
        /// <summary>
        /// 转成可以扩充的对象
        /// </summary> www.it165.net
        /// <param name="obj"></param>
        /// <returns></returns>
        public static dynamic ConvertToDynamic(object obj)
        {
            IDictionary<string, object> result = new ExpandoObject();
            foreach (PropertyDescriptor pro in TypeDescriptor.GetProperties(obj.GetType()))
            {
                result.Add(pro.Name, pro.GetValue(obj));
            }
            return result as ExpandoObject;
        }

        public static dynamic ConvertToDynamic(object obj, FeatureInfoX x)
        {
            Dictionary<string, string> myDict = new Dictionary<string, string>();
            foreach (XItems s in x.item)
            {
                if (s.enable)
                {
                    myDict.Add(s.name, s.dispName);
                }
            }

            IDictionary<string, object> result = new ExpandoObject();
            foreach (PropertyDescriptor pro in TypeDescriptor.GetProperties(obj.GetType()))
            {
                if (myDict.ContainsKey(pro.Name))
                {
                    if (String.IsNullOrEmpty(myDict[pro.Name]))
                        result.Add(pro.Name, pro.GetValue(obj));
                    else
                        result.Add(myDict[pro.Name], pro.GetValue(obj));
                }
            }
            return result as ExpandoObject;
        }

        public static dynamic ConvertToDynamic(object obj, string featureInfoXmlFile)
        {
            FeatureInfoX x = XmlHelper.XmlFileToObject<FeatureInfoX>(featureInfoXmlFile);
            return ConvertToDynamic(obj,x);
        }

        public static DataTable ConvertToDataTable(ExpandoObject obj)
        {
            IDictionary<string, object> myDict = obj;
            DataTable dt = new DataTable();
            dt.TableName = "myTable";
            foreach (KeyValuePair<string, object> kvp in myDict)
            {
                if (kvp.Value == null)
                {
                    dt.Columns.Add(kvp.Key, Type.GetType("System.String"));
                }
                else
                {
                    // dt.Columns.Add(kvp.Key, Type.GetType("System.String"));
                    dt.Columns.Add(kvp.Key, kvp.Value.GetType());
                }
            }
            return dt;
        }

        public static DataTable ConvertToDataTableWithData(ExpandoObject obj)
        {
            IDictionary<string, object> myDict = obj;
            DataTable dt = new DataTable();
            dt.TableName = "myTable";
            foreach (KeyValuePair<string, object> kvp in myDict)
            {
                if(kvp.Value == null)
                {
                    dt.Columns.Add(kvp.Key, Type.GetType("System.String"));
                }
                else
                {
                    // dt.Columns.Add(kvp.Key, Type.GetType("System.String"));
                    dt.Columns.Add(kvp.Key, kvp.Value.GetType());
                }
            }
            DataRow dr = dt.NewRow();         
            foreach (KeyValuePair<string, object> kvp in myDict)
            {
                dr[kvp.Key] = myDict[kvp.Key];
            }
            dt.Rows.Add(dr);
            return dt;
        }


    }
}
